<template>
<div class="app-container calendar-list-container">

  <el-row>
    <el-col :span="4" style='margin-top:15px;'>

    <el-select class="filter-item" v-model="listQuery.sessions" placeholder="请选择届别" @change="jieChange">
      <el-option v-for="item in  jieOptions" :key="item" :label="'第' + item +'届'" :value="item"> </el-option>
    </el-select>

      <el-tree
        class="filter-tree"
        :data="treeData"
        node-key="type"
        highlight-current
        :props="defaultProps"
        :filter-node-method="filterNode"
        :default-expanded-keys="['0']"
        ref="menuTree"
        @node-click="getNodeData">
      </el-tree>
    </el-col>
    <el-col :span="20" style='margin-top:15px;'>
      <el-card class="box-card">
        <div class="talent-list-wrap">
            <div class="filter-container">
                <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入学校名称点击搜索" v-model="listQuery.schoolName"> </el-input>
                <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
                <el-button class="filter-item fr" type="primary" @click="handlerAdd">添加</el-button>
                <!-- <el-button type="primary" v-if="menuManager_btn_edit" icon="edit" @click="handlerEdit">批量导入</el-button>
                <el-button type="primary" v-if="menuManager_btn_del" icon="delete" @click="handleDelete">批量导出</el-button> -->
              </div>

            <el-table :data="list" v-loading.body="listLoading" border fit highlight-current-row style="width: 100%">
                <el-table-column align="center" label="序号" width="65"> <template slot-scope="scope">
                      <span>{{scope.row.id}}</span>
                    </template> 
                </el-table-column>
                <el-table-column width="250" align="center" label="所在院校"> <template slot-scope="scope">
                    <span>{{scope.row.schoolName}}</span>
                  </template>
                </el-table-column>
                <el-table-column width="100" align="center" label="所在区县"> <template slot-scope="scope">
                      <span>{{scope.row.areaName}}</span>
                    </template> 
                </el-table-column>
                <el-table-column width="100" align="center" label="届别"> <template slot-scope="scope">
                      <span>{{scope.row.sessions}}</span>
                    </template> 
                </el-table-column>
                <el-table-column width="110" align="center" label="负责人"> <template slot-scope="scope">
                        <span>{{scope.row.principalMen}}</span>
                      </template> 
                </el-table-column>
                <el-table-column width="120" align="center" label="负责人电话"> <template slot-scope="scope">
                        <span>{{scope.row.principalMenPhone}}</span>
                      </template> 
                </el-table-column>
                <el-table-column align="center" label="操作" width="200"> <template slot-scope="scope">
                    <el-button size="small" type="success" @click="handleUpdate(scope.row)">编辑
                    </el-button>
                    <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除
                    </el-button>
                  </template> 
                </el-table-column>
              </el-table>
              <div v-show="!listLoading" class="pagination-container">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
              </div>
        </div>
      </el-card>
    </el-col>
  </el-row>

  <!--添加学校弹框 START-->
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
    <el-form :model="form" :rules="rules" ref="form" label-width="100px">

      <el-form-item label="届别" label-suffix="el-icon-search" prop="sessions">
        <el-select class="filter-item" style="width: 100%;" v-model="form.sessions" placeholder="请选择届别">
          <el-option v-for="item in  jieOptions" :key="item" :label="'第' + item +'届'" :value="item"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="选择区县">
        <el-select class="filter-item" style="width: 100%;" v-model="form.areaName" placeholder="请选择区县" value-key="id" @change="areaSelectChange">
          <el-option v-for="item in  areaOptions" :key="item.id" :label="item.name" :value="item"> </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="学校名称" prop="schoolName">
        <el-input v-model="form.schoolName" placeholder="请输入学校名称"></el-input>
      </el-form-item>
      <el-form-item label="负责人" prop="principalMen">
        <el-input v-model="form.principalMen" placeholder="请输入负责人"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="principalMenPhone">
        <el-input v-model="form.principalMenPhone" placeholder="请输入联系电话"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button v-if="dialogStatus=='create'" type="primary" @click="create('form')">确 定</el-button>
      <el-button v-else type="primary" @click="update('form')">确 定</el-button>
    </div>
  </el-dialog>
  <!--添加学校弹框 END-->
</div>
</template>

<script>
import {
  staticData
} from 'mock/staticData';
import { fetchJieOptionsObj, getAreaListObj } from 'api/admin/person/index';
import { fetchClassTreeObj } from 'api/admin/talent/index';
import {
  page,
  fetchTree, getObj, addObj, delObj, putObj
} from 'api/admin/school/index';
import { mapGetters } from 'vuex';
export default {
  name: 'menu',
  components: {

  },
  data() {
    return {
      filterText: '',
      list: null,
      total: null,
      formEdit: true,
      formAdd: true,
      formStatus: '',
      showElement: false,
      dialogFormVisible: false,
      typeOptions: ['menu', 'dirt'],
      listLoading: false,
      listQuery: {
        areaId: null,
        schoolName: null,
        sessions: null
      },
      treeData: [],
      jieOptions: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      labelPosition: 'right',
      form: {
        code: undefined,
        title: undefined,
        parentId: undefined,
        href: undefined,
        icon: undefined,
        orderNum: undefined,
        description: undefined,
        path: undefined,
        enabled: undefined,
        type: undefined,
        areaId: undefined
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入用户',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        username: [
          {
            required: true,
            message: '请输入账户',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            min: 5,
            max: 20,
            message: '长度在 5 到 20 个字符',
            trigger: 'blur'
          }
        ]
      },
      dialogStatus: '',
      textMap: {
        update: '修改学校',
        create: '添加学校'
      },
      areaOptions:[],
      currentId: -1,
      menuManager_btn_add: false,
      menuManager_btn_edit: false,
      menuManager_btn_del: false
    }
  },
  watch: {
    filterText(val) {
      this.$refs.menuTree.filter(val);
    }
  },
  created() {
    //初始化届下拉数据
    const p1 = this.getJieOptions();
    Promise.all([p1]).then(() => {
      this.getList();
    })
    this.menuManager_btn_add = this.elements['menuManager:btn_add'];
    this.menuManager_btn_del = this.elements['menuManager:btn_del'];
    this.menuManager_btn_edit = this.elements['menuManager:btn_edit'];
  },
  computed: {
    ...mapGetters([
      'elements'
    ])
  },
  methods: {
    getTree() {
      fetchTree(this.listQuery).then(data => {
        this.treeData = data;
      });
    },
    //获取届列表
    getJieOptions() {
     return fetchJieOptionsObj().then(res => {
        if(res && res.data){
          this.jieOptions = res.data;
          this.listQuery.sessions = res.data[0];
          this.getClassTree(res.data[0]);
        }
      });
    },
    jieChange (jieId) {
     this.jieId = jieId;
     this.getClassTree(jieId);
    },
    getClassTree(jieId) {
      var that = this;
      var param = {sessions: jieId};
      fetchClassTreeObj(param).then(res => {
        if(res && res.data){
          var classTree  =  JSON.parse(res.data.areaSchoolTreeList);
          this.filterClassTree(classTree);
          this.treeData = classTree;
        }
      });
    },
    //将树中的班级过滤掉
    filterClassTree(classTreeList){
      var that = this;
      classTreeList.forEach(item => {
        if(item.type == 2){
          item.children = [];
        }
        if(item.children && item.children.length > 0){
          that.filterClassTree(item.children);
        }
      });
    },
    handleFilter() {
      this.getList();
    },
    getList() {
      this.listLoading = true;
      page(this.listQuery)
        .then(response => {
          this.list = response.data.rows;
          this.total = response.data.total;
          this.listLoading = false;
        })
    },
    getAreaOptionsData() {
      //默认获取马鞍山的所有区
      var param = {pid: '368100142709874688'};
      getAreaListObj(param).then(response => {
        this.areaOptions = response.data;
      });
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    getNodeData(data) {
      if (data.type == 1) {
        this.listQuery.areaId = data.id;
        this.listQuery.schoolId = null;
        this.listQuery.classId = null;
      }else if(data.type == 2){
        this.listQuery.areaId = null;
        this.listQuery.schoolId = data.id;
        this.listQuery.classId = null;
      }else if(data.type == 3){
        this.listQuery.areaId = null;
        this.listQuery.schoolId = null;
        this.listQuery.classId = data.id;
      }else{
        this.listQuery.areaId = null;
        this.listQuery.schoolId = null;
        this.listQuery.classId = null;
      }
    },
    handlerEdit() {
      if (this.form.id) {
        this.formEdit = false;
        this.formStatus = 'update';
        
      }
    },
    handleUpdate(row) {
      this.getAreaOptionsData();
      getObj(row.id)
        .then(response => {
          this.form = response.data;
          this.dialogFormVisible = true;
          this.dialogStatus = 'update';
        });
    },
    handlerAdd() {
      this.getAreaOptionsData();
      this.resetForm();
      this.dialogStatus = 'create';
      this.dialogFormVisible = true;
    },
    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    cancel(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    handleDelete(row) {
      this.$confirm('确定删除此条记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          delObj(row.id)
            .then(() => {
              this.$notify({
                title: '成功',
                message: '删除成功',
                type: 'success',
                duration: 2000
              });
              const index = this.list.indexOf(row);
              this.list.splice(index, 1);
            });
        });
    },
    update() {
      putObj(this.form).then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
          title: '成功',
          message: '更新成功',
          type: 'success',
          duration: 2000
        });
      });
    },
    create() {
      addObj(this.form).then(() => {
        this.dialogFormVisible = false;
        this.getList();
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 2000
        });
      });
    },
    onCancel() {
      this.formEdit = true;
      this.formStatus = '';
    },
    resetForm() {
      this.form = {
        code: undefined,
        title: undefined,
        parentId: this.currentId,
        href: undefined,
        icon: undefined,
        orderNum: undefined,
        description: undefined,
        path: undefined,
        enabled: undefined
      };
    }, 
    areaSelectChange (data) {
      this.form.areaId = data.id;
      this.form.areaName = data.name;
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.talent-list-wrap{
  .el-form-item{
    margin-bottom: 10px;
  }
}

</style>